package club.sh.generator.config;

import lombok.Data;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.Yaml;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Map;

/**
 * 代码生成器工具类
 * @author 林思浩
 * @date 2020/07/24 9:52
 */
@Component
@Data
public class ToolConfig {

    public static String url;
    public static String driverClassName;
    public static String username;
    public static String password;

    /**
     * 负责初始化db参数
     */
    static {
        try {
            URL resource = new URL(URLDecoder.decode(ToolConfig.class.getClassLoader().getResource("db.yml").toString(), "UTF-8"));
            if(resource != null) {
                Yaml yaml = new Yaml();
                Map map = yaml.load(new FileInputStream(resource.getFile()));
                url = (String) map.get("url");
                driverClassName = (String) map.get("driver-class-name");
                username = String.valueOf(map.get("username"));
                password = String.valueOf(map.get("password"));
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    /**
     * Dao、Service类去掉表前缀
     */
    public static final String NOT_TABLE_PREFIX="";

    /**
     * 项目包根路径
     */
    public static final String PROJECT_ROOT_PATH = System.getProperty("user.dir");

    /**
     * DAO、MAPPER包名称
     */
    public static final String MAPPER_PACKAGE="dao";

    /**
     * SERVICE包名称
     */
    public static final String SERVICE_PACKAGE="service";

    /**
     * SERVICE IMPL包名称
     */
    public static final String SERVICE_IMPL_PACKAGE="service.impl";

    /**
     * ENTITY包名称
     */
    public static final String ENTITY_PACKAGE="domain";

    /**
     * Controller包名称
     */
    public static final String CONTROLLER_PACKAGE="controller";

    /**
     * mapper.xml文件路径
     */
    public static final String XML_PATH="mybatis";

}
